#include "whaletwitterconnect.h"

#include <QtDebug>
#include <QUrl>
#include <QList>
#include <QString>

WhaleTwitterConnect::WhaleTwitterConnect( QObject * parent )
    : WhaleAbstractConnect( parent ) {
}

void WhaleTwitterConnect::search( QString query ) {
    QUrl url( "http://socialwhale.com/search.json" );
    url.addQueryItem( "show_user", "true" );
    url.addQueryItem( "q", query );

    WhaleAbstractConnect::watch( url, this, SLOT( searchCallback( QVariant ) ) );
}

void WhaleTwitterConnect::searchCallback( QVariant reply ) {
    QVariantMap results = reply.toMap();
    emit this->newData( results[ "results" ].toList(), "search" );
}

void WhaleTwitterConnect::watch( QUrl url, QString streamName ) {
    this->setCurrentStreamName( streamName );
    WhaleAbstractConnect::watch( url, this, SLOT( twitterCallback( QVariant ) ) );
}

void WhaleTwitterConnect::twitterCallback( QVariant results ) {
    emit this->newData( results.toList(), this->currentStreamName() );
}

void WhaleTwitterConnect::watchFriendsTimeline() {
    this->watch( QUrl( "http://socialwhale.com/statuses/friends_timeline.json" ), "friendsTimeline" );
}

void WhaleTwitterConnect::watchMentions() {
    this->watch( QUrl( "http://socialwhale.com/statuses/mentions.json" ), "mentions" );
}

void WhaleTwitterConnect::watchDirectMessages() {
    this->watch( QUrl( "http://socialwhale.com/direct_messages.json" ), "directMessages" );
}

void WhaleTwitterConnect::watchRetweetsOfMe() {
    this->watch( QUrl( "http://socialwhale.com/statuses/retweets_of_me.json" ), "retweetsOfMe" );
}

void WhaleTwitterConnect::following() {
    this->watch( QUrl( "http://socialwhale.com/statuses/friends.json" ), "friends" );
}

void WhaleTwitterConnect::followers() {
    this->watch( QUrl( "http://socialwhale.com/statuses/followers.json" ), "followers" );
}
